package com.tongzhan.service;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.entity.xiangxian.UserInfo;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.utils.ThreadLocalUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author hongjianbin
 * @date 2020/7/20
 */
@Service
public class BaseInfoService extends BaseShareService {

    @Autowired
    private SysDictService sysDictService;

    public long addExamplepoint(ExamplePoint examplePoint) {
        User user = findUserById(examplePoint.getAddUserId());
        if(user!=null){
            examplePoint.setAddUserName(user.getUserName());
        }
        examplePoint.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        examplePoint.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = examplePointRepository.insertSelective(examplePoint);
        if(l>0){
            return  1;
        }
        return 0;
    }

    public ExamplePoint getdetail(String epid) {
        ExamplePoint examplePoint = examplePointRepository.findByPrimaryKey(epid);
        return examplePoint;
    }

    public Page<ExamplePoint> epList(String name, Integer page, Integer size) {
        Query query = new Query(BaseCriteria.where("is_del").is(0));
        if (Strings.isNotBlank(name)) {
            query.addCriteria(BaseCriteria.where("name").like(name));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        Page<ExamplePoint> all = examplePointRepository.findPage(query, of);
        return all;
    }

    public long addUserinfo(UserInfo userInfo) {
        userInfo.setTime(new Date());
        long l = userInfoRepository.insertSelective(userInfo);
        if(l>0){
            User user = new User();
            user.setUserName(userInfo.getPhone());
            user.setPassWord(Md5Utils.getMD5("123456".getBytes()));
            user.setIsDelete(0);
            user.setCreateTime(LocalDateTime.now());
            SnowFlakeUtils snowFlake = new SnowFlakeUtils(4, 4);
            user.set_id(String.valueOf(snowFlake.nextId()));
            user.setUserId(String.valueOf(snowFlake.nextId()));
            user.setTelephone(userInfo.getPhone());
            user.setType(3);
            user.setStreetCode(userInfo.getStreetCode());
            user.setVillageCode(userInfo.getVillageCode());
            userRepository.insert(user);
            return 1;
        }
        return 0;
    }

    public UserInfo userInfoDetail(String userId) {
        UserInfo userInfo = userInfoRepository.findByPrimaryKey(userId);
        //typeName改为新建用户时输入的内容
//        if (userInfo != null) {
//            userInfo.setTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.XIANGXIAN_USERINFO_TYPE, userInfo.getType()));
//        }
        return userInfo;
    }

    public Page<UserInfo> userInfoList(String name, Integer page, Integer size,String streetCode,String villageCode) {
        Query query = new Query(BaseCriteria.where("is_del").is(0));
        if (Strings.isNotBlank(name)) {
            query.addCriteria(BaseCriteria.where("name").like(name));
        }
        if (Strings.isNotBlank(streetCode)){
            query.addCriteria(BaseCriteria.where("streetCode").like(streetCode));
//            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
//                query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
//            }
        }
        if (Strings.isNotBlank(villageCode)){
            query.addCriteria(BaseCriteria.where("villageCode").like(villageCode));
//            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
//                query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
//            }
        }
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        Page<UserInfo> all = userInfoRepository.findPage(query, of);
        if (!CollectionUtils.isEmpty(all.getContent())) {
            all.getContent().forEach(item->{
                item.setTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.XIANGXIAN_USERINFO_TYPE, item.getType()));
            });
        }
        return all;
    }

    public long addAction(ActionCenter center) {
        center.setIs_del(0);
        center.setIs_pass(0);

        User userById = findUserById(center.getAdd_userid());
        if(userById!=null){
            center.setAdd_username(userById.getUserName());
        }
        center.setType(3);
        center.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        center.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());

        long l = actionCenterRepository.insertSelective(center);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(1, center.getAid()));
            return  1;
        }
        return 0;
    }

    public ActionCenter actionDetail(String aid, String userId) {
        ActionCenter center = actionCenterRepository.findByPrimaryKey(aid);
        if (center != null) {
            center.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, center.getActionTypeId()));
        }
        //标记角标已读
        markPromptService.read(aid,userId);
        return center;
    }

    public Page<ActionCenter> actionList(Integer page, Integer size) {
        Query query = new Query(BaseCriteria.where("is_del").is(0).and("type").is(3));
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionCenter> all = actionCenterRepository.findPage(query, of);

        if (!CollectionUtils.isEmpty(all.getContent())) {
            all.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, item.getActionTypeId()));
            });
        }

        return all;
    }

    public long findActionByName(String name,Integer type) {
        long count = actionCenterRepository.count(new Query().addCriteria(BaseCriteria.where("name").is(name).and("type").is(type).and("is_del").is(0)));
        return count;
    }


    public long findExamplepointByName(String name,Integer type) {
        long count = examplePointRepository.count(new Query().addCriteria(BaseCriteria.where("name").is(name).and("type").is(type).and("is_del").is(0)));
        return count;
    }
}

